package com.CST.service.impl.ys;

import com.CST.common.constant.Constants;
import com.CST.dto.ys.YsDetailDTO;
import com.CST.entity.cb.BdSysUser;
import com.CST.entity.ys.YsAssetDetail;
import com.CST.mapper.cst.cb.BdSysUserMapper;
import com.CST.mapper.cst.ys.YsAssetDetailMapper;
import com.CST.service.ys.YsAssetDetailService;
import com.CST.util.DateStringUtil;
import com.CST.util.ExcelTransfer;
import com.CST.util.HttpUtil;
import com.CST.util.PageHelperTool;
import com.CST.vo.ys.AccountDetailVO;
import com.CST.vo.ys.AssetDetailVO;
import com.CST.vo.ys.ExpenseVO;
import com.CST.vo.ys.YsAssetDetailVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qtx
 * @since 2023-04-21
 */
@Service
public class YsAssetDetailServiceImpl extends ServiceImpl<YsAssetDetailMapper, YsAssetDetail> implements YsAssetDetailService {

    private final YsAssetDetailMapper dao;

    @Autowired
    private BdSysUserMapper userMapper;

    @Autowired
    private ExcelTransfer<AssetDetailVO> excelTransfer;

    public YsAssetDetailServiceImpl(YsAssetDetailMapper dao) {
        this.dao = dao;
    }

    @Override
    public YsAssetDetailVO listAssetInfo(YsDetailDTO dto) {
        Optional.ofNullable(dto.getStartTime())
                .orElseThrow(()->new RuntimeException("起始时间不能为空"));
        Optional.ofNullable(dto.getEndTime())
                .orElseThrow(()->new RuntimeException("截止时间不能为空"));
        Optional.ofNullable(dto.getNum())
                .orElseThrow(()->new RuntimeException("页码不能为空"));
        Optional.ofNullable(dto.getSize())
                .orElseThrow(()->new RuntimeException("页大小不能为空"));
        if(!dto.getStartTime().split("-")[0].equals(dto.getEndTime().split("-")[0])){
            throw  new RuntimeException("年份不能跨越");
        }
        dto.setStart(DateStringUtil.transformer(dto.getStartTime()));
        dto.setEnd(DateStringUtil.transformer(dto.getEndTime()));

        BdSysUser user = userMapper.getUserById(HttpUtil.getRequestHeaderInfo(Constants.USER_ID));
        // todo  临时权限
        if (!"ALL".equals(user.getDeptCode())){
            dto.setDeptCode(user.getDeptCode());
        }
        List<AssetDetailVO> assetDetailVO  = dao.listAssetInfo(dto);
        PageInfo<AssetDetailVO> pageInfo = PageHelperTool.
                excutePageInfo(assetDetailVO, dto.getNum(), dto.getSize());
        List<ExpenseVO> expenseVOList = dao.listExpense(dto);
        BigDecimal total = expenseVOList.stream().
                map(ExpenseVO::getAMOUNT).reduce(BigDecimal.ZERO,BigDecimal::add);
        ExpenseVO evo = new ExpenseVO();
        evo.setSort(0);
        evo.setSubjectName("总费用");
        evo.setAMOUNT(total);
        expenseVOList.add(evo);
        YsAssetDetailVO vo = new YsAssetDetailVO();
        vo.setPageInfo(pageInfo);
        vo.setExpenseVOList(expenseVOList);
        vo.setTotal(total);
        return vo;
    }

    @Override
    public String exportManageAssetList(YsDetailDTO dto,HttpServletResponse response) {
        Optional.ofNullable(dto.getStartTime())
                .orElseThrow(()->new RuntimeException("起始时间不能为空"));
        Optional.ofNullable(dto.getEndTime())
                .orElseThrow(()->new RuntimeException("截止时间不能为空"));
        if(!dto.getStartTime().split("-")[0].equals(dto.getEndTime().split("-")[0])){
            throw  new RuntimeException("年份不能跨越");
        }
        try{
            dto.setStart(DateStringUtil.transformer(dto.getStartTime()));
            dto.setEnd(DateStringUtil.transformer(dto.getEndTime()));

            BdSysUser user = userMapper.getUserById(HttpUtil.getRequestHeaderInfo(Constants.USER_ID));
            // todo  临时权限
            if (!"ALL".equals(user.getDeptCode())){
                dto.setDeptCode(user.getDeptCode());
            }

            excelTransfer.exportExcel(response,dao.listAssetInfo(dto),"管理执行明细(资产类)","sheet",AssetDetailVO.class);
            return "导出成功";
        }catch (Exception e){
            throw new RuntimeException("导出失败");
        }
    }

    @Override
    public YsAssetDetailVO listBusinessAssetInfo(YsDetailDTO dto) {
        Optional.ofNullable(dto.getStartTime())
                .orElseThrow(()->new RuntimeException("起始时间不能为空"));
        Optional.ofNullable(dto.getEndTime())
                .orElseThrow(()->new RuntimeException("截止时间不能为空"));
        Optional.ofNullable(dto.getNum())
                .orElseThrow(()->new RuntimeException("页码不能为空"));
        Optional.ofNullable(dto.getSize())
                .orElseThrow(()->new RuntimeException("页大小不能为空"));
        if(!dto.getStartTime().split("-")[0].equals(dto.getEndTime().split("-")[0])){
            throw  new RuntimeException("年份不能跨越");
        }
        dto.setStart(DateStringUtil.transformer(dto.getStartTime()));
        dto.setEnd(DateStringUtil.transformer(dto.getEndTime()));
        BdSysUser user = userMapper.getUserById(HttpUtil.getRequestHeaderInfo(Constants.USER_ID));
        // todo  临时权限
        if (!"ALL".equals(user.getDeptCode())){
            dto.setDeptCode(user.getDeptCode());
        }
        List<AssetDetailVO> assetDetailVO  = dao.listBusinessAssetInfo(dto);
        PageInfo<AssetDetailVO> pageInfo = PageHelperTool.
                excutePageInfo(assetDetailVO, dto.getNum(), dto.getSize());
        List<ExpenseVO> expenseVOList = dao.listBusinessExpense(dto);
        BigDecimal total = expenseVOList.stream().
                map(ExpenseVO::getAMOUNT).reduce(BigDecimal.ZERO,BigDecimal::add);
        ExpenseVO evo = new ExpenseVO();
        evo.setSort(0);
        evo.setSubjectName("总费用");
        evo.setAMOUNT(total);
        expenseVOList.add(evo);
        YsAssetDetailVO vo = new YsAssetDetailVO();
        vo.setPageInfo(pageInfo);
        vo.setExpenseVOList(expenseVOList);
        vo.setTotal(total);
        return vo;
    }

    @Override
    public AssetDetailVO getAssetByNum(Integer num,Integer yard) {
        Optional.ofNullable(num)
                .orElseThrow(()->new RuntimeException("编码不能为空"));
        Optional.ofNullable(yard)
                .orElseThrow(()->new RuntimeException("院区不能为空"));

        return dao.getAssetByNum(num, yard);
    }

    @Override
    public AssetDetailVO getBusinessAssetByNum(Integer num, Integer yard) {
        Optional.ofNullable(num)
                .orElseThrow(()->new RuntimeException("编码不能为空"));
        Optional.ofNullable(yard)
                .orElseThrow(()->new RuntimeException("院区不能为空"));

        return dao.getBusinessAssetByNum(num, yard);
    }

    @Override
    public String exportBusinessAssetInfoList(YsDetailDTO dto, HttpServletResponse response) {
        Optional.ofNullable(dto.getStartTime())
                .orElseThrow(()->new RuntimeException("起始时间不能为空"));
        Optional.ofNullable(dto.getEndTime())
                .orElseThrow(()->new RuntimeException("截止时间不能为空"));
        if(!dto.getStartTime().split("-")[0].equals(dto.getEndTime().split("-")[0])){
            throw  new RuntimeException("年份不能跨越");
        }
        dto.setStart(DateStringUtil.transformer(dto.getStartTime()));
        dto.setEnd(DateStringUtil.transformer(dto.getEndTime()));

        BdSysUser user = userMapper.getUserById(HttpUtil.getRequestHeaderInfo(Constants.USER_ID));
        // todo  临时权限
        if (!"ALL".equals(user.getDeptCode())){
            dto.setDeptCode(user.getDeptCode());
        }

        excelTransfer.exportExcel(response, dao.listAssetInfo(dto), "业务预算执行明细(资产类)", "sheet",
                AssetDetailVO.class);
        return "导出成功";
    }

}
